<?php

class PhuongXa_Model extends MyModel
{
    
    function __construct()
    {
        parent::__construct();
    }
    
    function getPhuongXa($maPhuongXa)
    {
        $str = "SELECT maphuongxa, tenphuongxa, maquanhuyen 
                FROM phuongxa 
                WHERE maphuongxa = '{$maPhuongXa}'";
                
        $query = mysql_query($str);
        return $query;
    }
    
    function getAllPhuongXa()
    {
        $str = "SELECT phuongxa.maphuongxa, phuongxa.tenphuongxa, quanhuyen.tenquanhuyen
                FROM phuongxa
                INNER JOIN quanhuyen
                ON phuongxa.maquanhuyen=quanhuyen.maquanhuyen
                ORDER BY phuongxa.maphuongxa";
                
        $query = mysql_query($str);
        return $query;        
    }
    
    function getAllQuanHuyen()
    {
        $str = "SELECT maquanhuyen, tenquanhuyen 
                FROM quanhuyen";
                
        $query = mysql_query($str);
        return $query;        
    }
    
    function checkExist($action, $maPhuongXa, $tenPhuongXa)
    {
        switch($action)
        {
            case "add":
                $str = "SELECT * 
                        FROM phuongxa 
                        WHERE maphuongxa = '{$maPhuongXa}'";
                        
                $query = mysql_query($str);
                $result = mysql_num_rows($query) > 0 ? true : false;
                if($result == false)
                {
                    $str = "SELECT * 
                            FROM phuongxa 
                            WHERE tenphuongxa = '{$tenPhuongXa}'";
                            
                    $query = mysql_query($str);
                    return mysql_num_rows($query) > 0 ? true : false;            
                }
                else 
                    return true;
                break;
                
            case "edit":
                $str = "SELECT * 
                        FROM phuongxa 
                        WHERE tenphuongxa = '{$tenPhuongXa}'";
                        
                $query = mysql_query($str);
                return mysql_num_rows($query) > 0 ? true : false;
                break;
        }
    }
    
    function addPhuongXa($maPhuongXa, $tenPhuongXa, $maQuanHuyen)
    {
        $str = "INSERT INTO PhuongXa 
                VALUES('{$maPhuongXa}', '{$tenPhuongXa}', '{$maQuanHuyen}')";
                
        mysql_query($str);
    }
    
    function editPhuongXa($maPhuongXa, $tenPhuongXa, $maQuanHuyen)
    {
        $str = "UPDATE phuongxa 
                SET tenphuongxa = '{$tenPhuongXa}', maquanhuyen = '{$maQuanHuyen}'  
                WHERE maphuongxa = '{$maPhuongXa}'";
                
        mysql_query($str);
    }
    
    function checkPhuongXaStatus($maPhuongXa)
    {
        $str = "SELECT maphuongxa 
                FROM thonxom 
                WHERE maphuongxa = '{$maPhuongXa}'";
                
        $query = mysql_query($str);
        return mysql_num_rows($query) > 0 ? true : false;
    }
    
    function deletePhuongXa($maPhuongXa)
    {
        $str = "DELETE FROM phuongxa 
                WHERE maphuongxa = '{$maPhuongXa}'";
                
        mysql_query($str);
    }
    
}

?>